
/*

*/

#include "include/renderBMP.h"
#include "include/types.h"

#include <curses.h>
#include <iostream>
#include <ncurses.h>
#include <string>
#include <vector>

#define KEY_W 119
#define KEY_A 97
#define KEY_S 115
#define KEY_D 100


using namespace std;

int menuConfig(int y, int x, int num);

void renderBMPScreen(vector<vector<vector<uchar8>>> &BMPmap, int y, int x) {

    vector<vector<char>> map(BMPmap.size(), vector<char>(BMPmap[0].size(), '-'));

    int highlightX = 0;
    int highlightY = 0;
    int choice;

    noecho();
    curs_set(0);

    do {
        for (unsigned long int i = 0; i < BMPmap.size(); i++) {
            for (unsigned long int j = 0; j < BMPmap[i].size(); j++) {
                if ((int)i == highlightY && (int)j == highlightX ) {
                    wattron(stdscr, A_REVERSE);
                }

                if (BMPmap[i][j][0] > BMPmap[i][j][1] && BMPmap[i][j][0] > BMPmap[i][j][2])
                    map[i][j] = 'R';
                else if (BMPmap[i][j][1] > BMPmap[i][j][0] && BMPmap[i][j][1] > BMPmap[i][j][2])
                    map[i][j] = 'B';
                else if (BMPmap[i][j][2] > BMPmap[i][j][1] && BMPmap[i][j][2] > BMPmap[i][j][0])
                    map[i][j] = 'G';
                else if (BMPmap[i][j][0] == BMPmap[i][j][1] && BMPmap[i][j][0] != BMPmap[i][j][2])
                    map[i][j] = 'M';
                else if (BMPmap[i][j][0] != BMPmap[i][j][1] && BMPmap[i][j][0] == BMPmap[i][j][2])
                    map[i][j] = 'M';
                else if (BMPmap[i][j][1] == BMPmap[i][j][2] && BMPmap[i][j][0] != BMPmap[i][j][1])
                    map[i][j] = 'M';


                mvwprintw(stdscr, i, j, "%c", map[i][j]);


                wattroff(stdscr, A_REVERSE);
            }
        }

        choice = wgetch(stdscr);
        switch (choice) {
        case KEY_W:
            highlightY--;
            if (highlightY == -1) {
                highlightY = 0;
            }
            break;
        case KEY_S:
            highlightY++;
            if (highlightY == (int)BMPmap.size()) {
                highlightY = (int)BMPmap.size()-1;
            }
            break;
        case KEY_A:
            highlightX--;
            if (highlightX == -1) {
                highlightX = 0;
            }
            break;
        case KEY_D:
            highlightX++;
            if (highlightX == (int)BMPmap[0].size()) {
                highlightX = (int)BMPmap[0].size()-1;
            }
            break;
        default:
            break;
        }
        
        if (choice == 10) {
            BMPmap[highlightY][highlightX][0] = menuConfig(y, x, 0);
            BMPmap[highlightY][highlightX][1] = menuConfig(y, x, 1);
            BMPmap[highlightY][highlightX][2] = menuConfig(y, x, 2);
        } else if (choice == 27) {
            break;
        }
        
    } while(1);

}


int menuConfig(int yWin, int xWin, int num) {
    const string textColorRed = "Color red: ";
    const string textColorGreen = "Color green: ";
    const string textColorBlue = "Color blue: ";
    int inpRGB;

    int y = 5;
    int x = 25;

    WINDOW *inputRGB = newwin(y, x, (yWin / 2) - (y / 2), (xWin / 2) - (x / 2));
    box(inputRGB, 0, 0);
    refresh();
    wrefresh(inputRGB);
    keypad(inputRGB, true);
    echo();
    
    do {
        if (num == 0)
            mvwprintw(inputRGB, y/2, (x/2)-(textColorRed.size()/2), "%s", textColorRed.c_str());
        else if (num == 1)
            mvwprintw(inputRGB, y/2, (x/2)-(textColorGreen.size()/2), "%s", textColorGreen.c_str());
        else if (num == 2)
            mvwprintw(inputRGB, y/2, (x/2)-(textColorBlue.size()/2), "%s", textColorBlue.c_str());
        
        wscanw(inputRGB, "%d", &inpRGB);

        if (inpRGB >= 0 && inpRGB <= 255)
            break;

    }while (1);

    if (num == 2) {
        clear();
        noecho();
    }

    delwin(inputRGB);
    return inpRGB;
}